
import com.ssd.mixly.service.IMqttService;
import com.ssd.mixly.service.impl.MqttServiceImpl;

import java.util.Scanner;

public class MqttClientApp {

    public static void main(String[] args) {
        // 创建并初始化MQTT服务
        IMqttService service = new MqttServiceImpl();
        try {
            String[] topic={"send","pay"};
            service.listen(topic); // 开始监听ESP8266主题

            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入舵机编号，输入exit退出:");

            while (true) {
                String inputLine = scanner.nextLine();

                if ("exit".equalsIgnoreCase(inputLine.trim())) {
                    // 如果用户输入的是 "exit"，则断开连接并退出循环
                    break;
                }

                try {
                    String msg = inputLine.trim();
                    service.send("send", msg); // 只传递舵机编号
                } catch (NumberFormatException e) {
                    System.out.println("无效输入，请输入正确的舵机编号");
                }
            }

            scanner.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 确保即使发生异常，也尝试优雅地关闭资源和服务
            try {
                if (service instanceof AutoCloseable) {
                    ((AutoCloseable) service).close();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
}